package com.ruoyi.client.fxiaoke.utils;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;

public class Test {

	/**
	 * 级联的与普通的是一样的，value递增
	 * @param dataDesc
	 * @param fieldName
	 * @return
	 */
	public static Map<String, String> genOptionsMap(
			Map<String, Map<String, Object>> dataDesc, String fieldName) {
		Map<String, String> optionsMap = Maps.newHashMap();
		Map<?, ?> fieldDescMap = (Map<?, ?>) dataDesc.get("fields").get(
				fieldName);
		if (fieldDescMap == null || fieldDescMap.isEmpty()) {
			return optionsMap;
		}
		Object options = fieldDescMap.get("options");
		if (options != null) {
			List<Map<String, Object>> optionList = (List<Map<String, Object>>) options;
			if (optionList != null && !optionList.isEmpty()) {
				for (Map<String, Object> option : optionList) {
					optionsMap.put((String) option.get("value"),
							(String) option.get("label"));
					List<Map<String, Object>> childOptions = (List<Map<String, Object>>) option
							.get("child_options");
					if (childOptions != null) {
						for (Map<String, Object> item : childOptions) {
							optionsMap.put((String) item.get("value"),
									(String) item.get("label"));
						}
					}
				}
			}
		}
		return optionsMap;
	}

	public static String genOptionLabelWithChild(Map<String, Map<String, Object>> dataDesc,
			String fieldName, String value) {
		Map<?, ?> fieldDescMap = (Map<?, ?>) dataDesc.get("fields").get(fieldName);
		if (fieldDescMap == null || fieldDescMap.isEmpty()) {
			return "";
		}
		Object options = fieldDescMap.get("options");
		if (options == null) {
			return "";
		}
		List<Map<String, Object>> optionList = (List<Map<String, Object>>) options;
		if (optionList != null && !optionList.isEmpty()) {
			for (Map<String, Object> option : optionList) {
				String label = (String) option.get("label");
				List<Map<String, Object>> childOptions = (List<Map<String, Object>>) option.get("child_options");
				if (childOptions != null) {
					for (Map<String, Object> item : childOptions) {
						if (value.equals((String) item.get("value"))) {
							return label.concat("/").concat((String) item.get("label"));
						}
					}
				}
			}
		}
		return "";
	}
	
	public static String genOptionLabel(Map<String, Map<String, Object>> dataDesc,
			String fieldName, String value) {
		Map<?, ?> fieldDescMap = (Map<?, ?>) dataDesc.get("fields").get(fieldName);
		if (fieldDescMap == null || fieldDescMap.isEmpty()) {
			return "";
		}
		Object options = fieldDescMap.get("options");
		if (options == null) {
			return "";
		}
		List<Map<String, Object>> optionList = (List<Map<String, Object>>) options;
		if (optionList != null && !optionList.isEmpty()) {
			for (Map<String, Object> option : optionList) {
				String label = (String) option.get("label");
				String value1 = (String) option.get("value");
				if(value.equals(value1)){
					return label;
				}
			}
		}
		return "";
	}
}
